/**
 * Created by Administrator on 2017/12/19.
 */
import cookie from "js-cookie";
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import { addDreview, rewpun, result } from '../../services/organization';

export default {
  namespace: 'addappraisals',
  state: {
    Lists: [],
    rewpun: [],
    result: [],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if (pathname === '/work/democraticappraisal/addappraisal') {
          dispatch({
            type: 'results',
          }),
            dispatch({
              type: 'select',
            })
        }
      })
    }
  },

  effects: {
    * fetch({ payload }, { call, put, select }) {
      if (payload) {
        const data = yield call(addDreview, payload);
        console.log("data", data.data.code);
        if (data.data.code == 200) {
          Toast.success('创建成功！', 1);
          //跳转到列表页
          yield put(routerRedux.push('/work/democraticappraisal'));
        }
        else {
          Toast.fail(data.data.msg, 1);
        }
        yield put({
          type: 'success',
          payload: {
            Lists: data
          }
        });
      }
    },
    * select({ payload }, { call, put, select }) {
      const data = yield call(rewpun, payload);
      yield put({
        type: 'success',
        payload: {
          rewpun: data.data.ret
        }
      });
    },
    * results({ payload }, { call, put, select }) {
      const data = yield call(result, payload);
      yield put({
        type: 'success',
        payload: {
          result: data.data.ret
        }
      });
    },
  },
  reducers: {
    success(state, action) {
      return { ...state, ...action.payload };
    }
  }
};
